﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ServeurRSS_1_0.Source.ExceptionRss;
using HtmlAgilityPack;
using System.ServiceModel.Syndication;
using System.Net;
using System.Data.Linq;

namespace ServeurRSS_1_0.Source.Model
{
    public class ParsingModel
    {

        //SyndicationFeed fedd;
        //List<SyndicationItem> feddItems;

        public delegate void doAction(string elem);
        public delegate List<string> getElem(object elem);

        public List<string> getSrcImage(object elem)
        {
            List<string> listImg = new List<string>();
            try
            {

                if (!(elem is HtmlDocument))
                    return new List<string>();
                HtmlDocument doc = elem as HtmlDocument;
                if (doc != null && doc.DocumentNode != null)
                    foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//img"))
                    {
                        if (node.HasAttributes && node.Attributes["src"].Value != null && (node.Attributes["src"].Value.ToLower().Contains(".jpg") == true || node.Attributes["src"].Value.ToLower().Contains(".png") == true || node.Attributes["src"].Value.ToLower().Contains(".jpeg") == true))
                            listImg.Add(node.Attributes["src"].Value);
                    }
                         return listImg;
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception occured = " + e.Message + " stack = " + e.StackTrace);

            }
            return listImg;
        }

        public List<string> getLinksAndType(object elem)
        {
            List<string> listRes = new List<string>();
            try
            {

                if (!(elem is SyndicationItem))
                    return listRes;

                SyndicationItem item = elem as SyndicationItem;
                foreach (SyndicationLink elemLink in item.Links)
                    if (elemLink.MediaType != null && elemLink.Length > 0 && elemLink.MediaType != "")
                        listRes.Add(elemLink.Uri.OriginalString + "," + elemLink.MediaType);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception occured = " + e.Message + " stack = " + e.StackTrace);

            }
            return listRes;
        }

        public string getTextWithoutHtml(string html)
        {
            try
            {
                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(html);
                return doc.DocumentNode.InnerText != null ? doc.DocumentNode.InnerText : html;
            }
            catch
            {
                return html;
            }
        }


        public void ForEach(object html, getElem get, doAction action)
        {
            List<string> listImg = get(html);
            foreach (string stmp in listImg)
                action(stmp);
        }

        public List<Image> getListImageSyndication(System.Guid idItem, SyndicationItem item)
        {
            List<Image> res = new List<Image>();
            try
            {
                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(item.Summary.Text);
                ForEach(doc, getSrcImage, (str) =>
                {
                    Image resTmp = new Image
                    {
                        fk_id_Items = idItem,
                        id = System.Guid.NewGuid(),
                        url = str,
                        type = "htmlBaliseImg"
                    };
                    res.Add(resTmp);
                });

                ForEach(item, getLinksAndType, (str) =>
                {
                    string[] elem = str.Split(new char[] { ',' });
                    if (elem != null && elem.Count() > 0)
                    {
                        Image resTmp = new Image
                        {
                            fk_id_Items = idItem,
                            id = System.Guid.NewGuid(),
                            url = elem[0],
                            type = elem[1] != null ? elem[1] : "unknown"
                        };
                        res.Add(resTmp);
                    }

                });
                return res;
            }
            catch
            {
                return res;
            }
        }
    }
}